package com.example.app2024.viewpagerAD;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.example.app2024.R;

import java.util.ArrayList;

public class ViewpagerView extends AppCompatActivity
{
      
      private static final String TAG = ViewpagerView.class.getSimpleName();
      private ViewPager viewpager;
      private TextView tv_title;
      private LinearLayout ll_point_group;
      private  TextView textView002;
      
      //ListView的使用
      //1.在布局文件中定义ListView
      //2.在代码中实例化ListView
      //3.准备数据
      //4.设置适配器-item布局-绑定数据
      
      private ArrayList<ImageView> imageViews;
      
      // 图片资源ID
      private final int[] imageIds = new int[5];
      
      /**
       * 上一次高亮显示的位置
       */
      private int prePosition = 0;
      /**
       * 是否已经滚动
       */
      private boolean isDragging = false;
      
      // 图片标题集合
      private final String[] imageDescriptions = {"尚硅谷波河争霸赛！", "凝聚你我，放飞梦想！", "抱歉没座位了！", "7月就业名单全部曝光！", "平均起薪11345元"};
      
      private EditText et_input;
      private ImageView iv_down_arrow;
      private PopupWindow popupWindow;
      private ListView listview;
      private MyAdapter myAdapter;
      private ArrayList<String> msgs;
      
      private Handler handler = new Handler()
      {
            @Override
            public void handleMessage(Message msg)
            {
                  super.handleMessage(msg);
                  
                  int item = viewpager.getCurrentItem() + 1;
                  viewpager.setCurrentItem(item);
                  
                  //延迟发消息
                  handler.sendEmptyMessageDelayed(0, 4000);
            }
      };
      
      
      @Override
      protected void onCreate(Bundle savedInstanceState)
      {
            imageIds[0] = R.drawable.viewpager_a;
            imageIds[1] = R.drawable.viewpager_b;
            imageIds[2] = R.drawable.viewpager_c;
            imageIds[3] = R.drawable.viewpager_d;
            imageIds[4] = R.drawable.viewpager_e;
            
            textView002 = findViewById(R.id.tv_temp002);
            
            super.onCreate(savedInstanceState);
            setContentView(R.layout.viewpageractivity_main);
            viewpager = (ViewPager) findViewById(R.id.viewpager);
            tv_title = (TextView) findViewById(R.id.tv_title);
            ll_point_group = (LinearLayout) findViewById(R.id.ll_point_group);
            
            
            //ViewPager的使用
            //1.在布局文件中定义ViewPager
            //2.在代码中实例化ViewPager
            //3.准备数据
            imageViews = new ArrayList<>();
            for (int i = 0; i < imageIds.length; i++)
            {
                  
                  ImageView imageView = new ImageView(this);
                  imageView.setBackgroundResource(imageIds[i]);
                  
                  //添加到集合中
                  imageViews.add(imageView);
                  
                  //添加点
                  ImageView point = new ImageView(this);
                  point.setBackgroundResource(R.drawable.point_selector);
                  LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(8, 8);
                  if (i == 0)
                  {
                        point.setEnabled(true); //显示红色
                  }
                  else
                  {
                        point.setEnabled(false);//显示灰色
                        params.leftMargin = 8;
                  }
                  
                  
                  point.setLayoutParams(params);
                  
                  ll_point_group.addView(point);
            }
            //4.设置适配器(PagerAdapter)-item布局-绑定数据
            
            viewpager.setAdapter(new MyPagerAdapter());
            //设置监听ViewPager页面的改变
            viewpager.addOnPageChangeListener(new MyOnPageChangeListener());
            
            //设置中间位置
            int item = Integer.MAX_VALUE / 2 - Integer.MAX_VALUE / 2 % imageViews.size();//要保证imageViews的整数倍
            
            
            viewpager.setCurrentItem(item);
            
            tv_title.setText(imageDescriptions[prePosition]);
            
            //发消息
            handler.sendEmptyMessageDelayed(0, 3000);  //这一句应该是放在子线程中才是它真正的作用, 诸
            
            iv_down_arrow = (ImageView) findViewById(R.id.iv_down_arrow);
            et_input = (EditText) findViewById(R.id.et_input);
            
            et_input.setOnClickListener(new View.OnClickListener()
            {
                  @Override
                  public void onClick(View v)
                  {
                        if (popupWindow == null)
                        {
                              popupWindow = new PopupWindow(ViewpagerView.this);
                              popupWindow.setWidth(et_input.getWidth());
                              int height = DensityUtil.dip2px(ViewpagerView.this, 200);//dp->px
                              Toast.makeText(ViewpagerView.this, "height==" + height, Toast.LENGTH_SHORT).show();
                              popupWindow.setHeight(height);//px
                              
                              popupWindow.setContentView(listview);
                              popupWindow.setFocusable(true);//设置焦点
                        }
                        
                        popupWindow.showAsDropDown(et_input, 0, 0);
                  }
            });
            
            listview = new ListView(this);
            listview.setBackgroundResource(R.drawable.listview_background);
            //准备数据
            msgs = new ArrayList<>();
            for (int i = 0; i < 100; i++)
            {
                  msgs.add(i + "--aaaaaaaaaaaaaa--" + i);
            }
            myAdapter = new MyAdapter();
            listview.setAdapter(myAdapter);
            listview.setOnItemClickListener(new AdapterView.OnItemClickListener()
            {
                  @Override
                  public void onItemClick(AdapterView<?> parent, View view, int position, long id)
                  {
                        //1.得到数据
                        String msg = msgs.get(position);
                        //2.设置输入框
                        et_input.setText(msg);
                        
                        if (popupWindow != null && popupWindow.isShowing())
                        {
                              popupWindow.dismiss();
                              popupWindow = null;
                        }
                  }
            });
            
      }
      
      class MyAdapter extends BaseAdapter
      {
            
            @Override
            public int getCount()
            {
                  return msgs.size();
            }
            
            @Override
            public Object getItem(int position)
            {
                  return null;
            }
            
            @Override
            public long getItemId(int position)
            {
                  return 0;
            }
            
            @Override
            public View getView(int position, View convertView, ViewGroup parent)
            {
                  ViewHolder viewHolder;
                  if (convertView == null)
                  {
                        convertView = View.inflate(ViewpagerView.this, R.layout.pop_item_main, null);
                        viewHolder = new ViewHolder();
                        viewHolder.tv_msg = (TextView) convertView.findViewById(R.id.tv_msg);
                        viewHolder.iv_delete = (ImageView) convertView.findViewById(R.id.iv_delete);
                        convertView.setTag(viewHolder);
                  }
                  else
                  {
                        viewHolder = (ViewHolder) convertView.getTag();
                  }
                  
                  //根据位置得到数据
                  final String msg = msgs.get(position);
                  viewHolder.tv_msg.setText(msg);
                  
                  //设置删除
                  viewHolder.iv_delete.setOnClickListener(new View.OnClickListener()
                  {
                        @Override
                        public void onClick(View v)
                        {
                              //1.从集合中删除
                              msgs.remove(msg);
                              //2.刷新ui-适配器刷新
                              myAdapter.notifyDataSetChanged();//getCount()-->getView();
                              
                        }
                  });
                  return convertView;
            }
      }
      
      class ViewHolder
      {
            TextView tv_msg;
            ImageView iv_delete;
      }
      
      class MyOnPageChangeListener implements ViewPager.OnPageChangeListener
      {
            
            /**
             * 当页面滚动了的时候回调这个方法
             *
             * @param position             当前页面的位置
             * @param positionOffset       滑动页面的百分比
             * @param positionOffsetPixels 在屏幕上滑动的像数
             */
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels)
            {
            
            }
            
            /**
             * 当某个页面被选中了的时候回调
             *
             * @param position 被选中页面的位置
             */
            @Override
            public void onPageSelected(int position)
            {
                  int realPosition = position % imageViews.size();
                  //设置对应页面的文本信息
                  tv_title.setText(imageDescriptions[realPosition]);
                  
                  //把上一个高亮的设置默认-灰色
                  ll_point_group.getChildAt(prePosition).setEnabled(false);
                  //当前的设置为高亮-红色
                  ll_point_group.getChildAt(realPosition).setEnabled(true);
                  
                  prePosition = realPosition;
                  
                  
            }
            
            /**
             * 当页面滚动状态变化的时候回调这个方法
             * 静止->滑动
             * 滑动-->静止
             * 静止-->拖拽
             */
            @Override
            public void onPageScrollStateChanged(int state)
            {
                  if (state == ViewPager.SCROLL_STATE_DRAGGING)
                  {
                        isDragging = true;
                        handler.removeCallbacksAndMessages(null);
                        Log.e(TAG, "SCROLL_STATE_DRAGGING-------------------");
                  }
                  else if (state == ViewPager.SCROLL_STATE_SETTLING)
                  {
                        Log.e(TAG, "SCROLL_STATE_SETTLING-----------------");
                        
                  }
                  else if (state == ViewPager.SCROLL_STATE_IDLE && isDragging)
                  {
                        isDragging = false;
                        Log.e(TAG, "SCROLL_STATE_IDLE------------");
                        handler.removeCallbacksAndMessages(null);
                        handler.sendEmptyMessageDelayed(0, 4000);
                  }
                  
            }
      }
      
      class MyPagerAdapter extends PagerAdapter
      {
            
            
            /**
             * 得到图片的总数
             *
             * @return
             */
            @Override
            public int getCount()
            {
                  //            return imageViews.size();
                  return Integer.MAX_VALUE;
            }
            
            /**
             * 相当于getView方法
             *
             * @param container ViewPager自身
             * @param position  当前实例化页面的位置
             * @return
             */
            @Override
            public Object instantiateItem(ViewGroup container, final int position)
            {
                  
                  int realPosition = position % imageViews.size();
                  
                  final ImageView imageView = imageViews.get(realPosition);
                  container.addView(imageView);//添加到ViewPager中
                  //            Log.e(TAG, "instantiateItem==" + position + ",---imageView==" + imageView);
                  
                  imageView.setOnTouchListener(new View.OnTouchListener()
                  {
                        @Override
                        public boolean onTouch(View v, MotionEvent event)
                        {
                              switch (event.getAction())
                              {
                                    case MotionEvent.ACTION_DOWN://手指按下
                                          Log.e(TAG, "onTouch==手指按下");
                                          handler.removeCallbacksAndMessages(null);
                                          break;
                                    
                                    case MotionEvent.ACTION_MOVE://手指在这个控件上移动
                                          break;
                                    case MotionEvent.ACTION_CANCEL://手指在这个控件上移动
                                          Log.e(TAG, "onTouch==事件取消");
                                          //                            handler.removeCallbacksAndMessages(null);
                                          //                            handler.sendEmptyMessageDelayed(0,4000);
                                          break;
                                    case MotionEvent.ACTION_UP://手指离开
                                          Log.e(TAG, "onTouch==手指离开");
                                          handler.removeCallbacksAndMessages(null);
                                          handler.sendEmptyMessageDelayed(0, 4000);
                                          break;
                              }
                              return false;
                        }
                  });
                  
                  imageView.setTag(position);
                  imageView.setOnClickListener(new View.OnClickListener()
                  {
                        @Override
                        public void onClick(View v)
                        {
                              Log.e(TAG, "点击事件");
                              int position = (int) v.getTag() % imageViews.size();
                              String text = imageDescriptions[position];
                              Toast.makeText(ViewpagerView.this, "text==" + text, Toast.LENGTH_SHORT).show();
                              
                        }
                  });
                  
                  return imageView;
            }
            
            /**
             * 比较view和object是否同一个实例
             *
             * @param view   页面
             * @param object 这个方法instantiateItem返回的结果
             * @return
             */
            @Override
            public boolean isViewFromObject(View view, Object object)
            {
                  //            if(view == object){
                  //                return true;
                  //            }else{
                  //                return  false;
                  //            }
                  return view == object;
            }
            
            
            /**
             * 释放资源
             *
             * @param container viewpager
             * @param position  要释放的位置
             * @param object    要释放的页面
             */
            @Override
            public void destroyItem(ViewGroup container, int position, Object object)
            {
                  //            super.destroyItem(container, position, object);
                  //            Log.e(TAG, "destroyItem==" + position + ",---object==" + object);
                  container.removeView((View) object);
                  
            }
      }
}
